#include "StdAfx.h"
#include "ChatManager.h"
CChatManager g_chatmanger;

ChatClient::ChatClient( ChatInfo* chat ):m_chatinfo(chat)
{

}

ChatClient::~ChatClient()
{
	
}


CChatManager::CChatManager(void)
{
	m_client = NULL;
	m_main = NULL;
}

CChatManager::~CChatManager(void)
{
	ChatMap::iterator iter = m_chats.begin();
	for (; iter != m_chats.end(); iter++)
	{
		ChatInfo* chat = iter->second;
		if (chat->m_eventfilter != NULL)
			delete chat->m_eventfilter;
		if (chat->m_statefilter != NULL)
			delete chat->m_statefilter;
// 		if (chat->m_session)
// 		{
// 			if (m_client)
// 				m_client->disposeMessageSession(chat->m_session);
// 			else
// 				delete chat->m_session;
// 		
// 			chat->m_session = NULL;
// 		}
		if (chat->m_client)
		{
			chat->m_client->destory();
			delete chat->m_client;
		}
		delete chat;
	}
	m_chats.clear();
}

void CChatManager::handleMessageSession( MessageSession* session )
{
	if (session == NULL)
		return;
	JID jid;
	createChat(jid, session);
}

void CChatManager::handleMessage( const Message& msg, MessageSession* session /*= 0 */ )
{
	ChatInfo* chat = NULL;
	ChatMap::iterator iter = m_chats.begin();
	for (; iter != m_chats.end(); iter++)
	{
		if (iter->second->m_session == session)
		{
			chat = iter->second;
			break;
		}
	}
	if (chat == NULL)
		chat = createChat(msg.from(), session);
	else
	{
		if (chat->m_chatjid.full().empty())
		{
			chat->m_chatjid = msg.from();
			m_chats.erase(iter);
			m_chats.insert(std::make_pair(chat->m_chatjid, chat));
		}
		chat->m_recvlist.push_back(msg.body());
		if (m_main != NULL && IsWindow(m_main->m_hWnd))
			m_main->PostMessage(WM_ONMSG, (WPARAM)chat, 0);
	}
}


ChatInfo* CChatManager::createChat(const JID& jid, MessageSession *session )
{
	if (m_client == NULL)
		return NULL;

	ChatInfo* chat = getChat(jid);
	if (chat != NULL)
	{
		MessageSession* oldsession = chat->m_session;
		if (session != oldsession && session != NULL && oldsession != NULL)
		{
			chat->m_session = session;
			chat->m_session->registerMessageHandler(this);
			m_client->disposeMessageSession(oldsession);
			m_client->registerMessageSession(chat->m_session);
			chat->m_eventfilter->attachTo(chat->m_session);
			chat->m_statefilter->attachTo(chat->m_session);
		}
		else if (session != oldsession)
			return NULL;
	}
	else
	{
		chat = new ChatInfo();
		chat->m_chatjid = jid;
		if (session == NULL)
			chat->m_session = new MessageSession(m_client, jid, true, Message::Chat);
		else
			chat->m_session = session;
		chat->m_session->registerMessageHandler(this);
		m_chats.insert(std::make_pair(jid, chat));
		m_client->registerMessageSession(chat->m_session);
		chat->m_eventfilter = new MessageEventFilter(chat->m_session);
		chat->m_statefilter = new ChatStateFilter(chat->m_session);
	}
	
	return chat;
}

ChatInfo* CChatManager::getChat( const JID& jid )
{
	ChatMap::iterator iter = m_chats.find(jid);
	if (iter != m_chats.end())
		return iter->second;

	return NULL;
}

ChatInfo* CChatManager::getChat( MessageSession* session )
{
	ChatMap::iterator iter = m_chats.begin();
	for (; iter != m_chats.end(); iter++)
	{
		if (iter->second->m_session == session)
			return iter->second;
	}

	return NULL;
}

void CChatManager::sendMessage( const JID& jid, std::string msg )
{
	if (m_client == NULL)
		return;

	ChatInfo* chat = getChat(jid);
	if (chat == NULL)
		chat = createChat(jid, NULL);
	if (chat->m_session != NULL)
	{
		chat->m_session->send(msg);
	}
}

bool CChatManager::createChatClient( ChatInfo* chat )
{
	if (chat == NULL)
		return false;
	chat->m_client->create();

	return true;
}


